//
//  ST_VideoChatViewController.swift
//  btvideo
//
//  Created by mac on 2020/12/7.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON

class ST_VideoChatMatchingAnimateViewController: ST_BTLightBarViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        MatchForMeetDispatcher.shared.beginMatching(isVideo: false, controller: self)
        ST_MediaChatDispatcher.shared.matching_animate_controller = self
        
        title_nav_btn.setTitleForAllStates("视频速配")
        title_nav_btn.setTitleColorForAllStates(UIColor.white)
        
        
        own_navigation_bar.backgroundColor = .clear
        view.insertSubview(back_image_view, belowSubview: own_navigation_bar)
        back_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        setup_matching_u_i()
        
        ST_MediaChatDispatcher.shared.play_waiting_voice()
    }
    
    deinit {
        
        ST_MediaChatDispatcher.shared.stop_waiting_voice()
    }
    
//    MARK: - Setup UI
    fileprivate func setup_matching_u_i(){
        
        
        
        self.view.addSubview(first_animation_view)
        first_animation_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(35)
            make.right.equalToSuperview().offset(-35)
            make.height.equalTo(first_animation_view.snp.width)
            make.top.equalToSuperview().offset((104-20)/375.0*ScreenWidth+statusBarHeight)
        }
        
        self.view.addSubview(match_title_label)
        match_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(28)
            make.top.equalTo(first_animation_view.snp.bottom).offset(82)
        }
        
        self.view.addSubview(match_content_label)
        match_content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(40)
            make.top.equalTo(match_title_label.snp.bottom).offset(10)
        }
        
            
        self.first_animation_view.play { (f) in
            
            self.first_animation_view.isHidden = true

            self.view.addSubview(self.second_animation_view)
            self.second_animation_view.snp.makeConstraints { (make) in
                make.edges.equalTo(self.first_animation_view)
            }
            self.second_animation_view.play()
        }
    }
    
    fileprivate func remove_all_matching_u_i(){
        
        if match_title_label.superview == nil{
            return
        }
        
        first_animation_view.removeFromSuperview()
        
        match_title_label.removeFromSuperview()
        
        match_content_label.removeFromSuperview()
        
        if first_animation_view.isAnimationPlaying{
            first_animation_view.stop()
        }
        first_animation_view.removeFromSuperview()
        
        if second_animation_view.isAnimationPlaying{
            second_animation_view.stop()
        }
        second_animation_view.removeFromSuperview()
        
    }
    
    
    
//    MARK: - Properties
    fileprivate lazy var back_image_view:UIImageView = {
       let control = UIImageView.init(image: ST_ImageUtils.imageName("碰一碰背景"))
       return control
   }()
   
    fileprivate lazy var avatar_image_view_width = TIANCompatibleValue(value: 128)
   
    fileprivate lazy var avatar_image_view:UIImageView = {
          let control = UIImageView()
           control.contentMode = .scaleAspectFill
           control.cornerRadius = self.avatar_image_view_width/2.0
           control.borderColor = .white
           control.borderWidth = 2
           return control
   }()
   
    fileprivate lazy var nickname_label:UILabel = {
       let control = UILabel.init()
       control.text = ""
       control.textColor = UIColor.white
       control.textAlignment = .center
       control.font = UIFont.systemFont(ofSize: TIANCompatibleValue(value: 21), weight: UIFont.Weight.medium)
       return control
   }()
    
    fileprivate lazy var first_animation_view:LOTAnimationView = {
        
        guard  let path = Bundle.main.path(forResource: "supperzzleZoom", ofType: "json") else{
             return LOTAnimationView()
         }
        let animationControl = LOTAnimationView.init(filePath: path)
        
        animationControl.contentMode = UIView.ContentMode.scaleAspectFill
        return animationControl
    }()
    
    fileprivate lazy var second_animation_view:LOTAnimationView = {
        
        guard  let path = Bundle.main.path(forResource: "supperzzleZoomSeconds", ofType: "json") else{
             return LOTAnimationView()
         }
        let animationControl = LOTAnimationView.init(filePath: path)
        
        animationControl.contentMode = UIView.ContentMode.scaleAspectFill
        
        animationControl.loopAnimation = true
        
        return animationControl
    }()
    
    
    fileprivate lazy var match_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "正在匹配中..."
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()

    
    fileprivate lazy var match_content_label:UILabel = {
        let control = UILabel.init()
        control.text = "伴心酱正在帮你寻找在线视频速配的人呢\n请耐心等待哦~"
        control.textColor = .white
        control.numberOfLines = 2
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()

    
    
}
